package com.atguigu.gulimall.coupon.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author 阿良良木君
 * @email 1943773202@qq.com
 * @date 2022/4/7 20:32
 * @describe 请求接口环绕切面 监听所有controller方法的调用情况
 */
@Aspect //定义对象为切面类
@Component
public class MyAspect {

    private Logger logger = LoggerFactory.getLogger(MyAspect.class);

    /**
     * 定义切面
     */
    @Pointcut(value = "execution( * com.atguigu.gulimall.coupon.controller.*.*(..))")
    public void myPointcut(){

    }

    @Around("myPointcut()")
    public Object myLogger(ProceedingJoinPoint pjp) throws Throwable {

        //获取类名
        String className = pjp.getTarget().getClass().toString();
        //获取方法名
        String methodName = pjp.getSignature().getName();
        Object[] array = pjp.getArgs();
        ObjectMapper mapper = new ObjectMapper();

        logger.info("调用前："+className+":"+methodName+"传递的参数为："+mapper.writeValueAsString(array));
        Object obj = pjp.proceed();
        logger.info("调用后："+className+":"+methodName+"返回值："+mapper.writeValueAsString(obj));

        return obj;
    }

}
